package com.common.base.app.extras

import android.system.ErrnoException
import com.bumptech.glide.load.HttpException
import com.common.base.R
import com.common.base.utils.AppUtils
import com.google.gson.JsonParseException
import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException
import org.json.JSONException
import rxhttp.wrapper.exception.HttpStatusCodeException
import java.io.InterruptedIOException
import java.net.ConnectException
import java.net.SocketException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException
import javax.net.ssl.SSLHandshakeException

fun Throwable.getShowMsg() = when(this){
    is HttpException ->"${
        when(statusCode){
            401 -> "文件未授权或证书错误！"
            403 -> "服务器拒绝请求！"
            404 -> "服务器找不到请求的文件！"
            408 -> "请求超时，服务器未响应！"
            500 -> "服务器内部错误）服务器遇到错误，无法完成请求！"
            502 -> "服务器从上游服务器收到无效响应！"
            503 -> "服务器目前无法使用！"
            504 -> "服务器从上游服务器获取数据超时！"
            else -> "服务器错误！"
        }
    }，错误码：${statusCode}"
    is HttpStatusCodeException -> AppUtils.getString(R.string.network_framework_data_error2)
    is ParseException -> "json格式错误！"
    is JSONException -> "json解析错误！"
    is JsonParseException -> "json参数错误！"
    is SSLHandshakeException -> "证书验证失败！"
    is SocketTimeoutException -> "连接超时！"
    is ConnectException, is UnknownHostException,is InterruptedIOException,is SocketException -> AppUtils.getString(R.string.network_framework_data_error)
    is ErrnoException -> "网络不可访问！"
    is OnErrorNotImplementedException -> cause?.message?:"未知错误！"
    else -> message?:"未知错误！"
}